package net.koodar.forge.system.role.service.assembler;

import lombok.RequiredArgsConstructor;
import net.koodar.forge.system.menu.model.entity.Menu;
import net.koodar.forge.system.menu.manager.MenuManager;
import net.koodar.forge.system.role.model.dto.RoleParamDTO;
import net.koodar.forge.system.role.model.entity.Role;
import net.koodar.forge.system.role.model.vo.RoleVO;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author liyc
 */
@Component
@RequiredArgsConstructor
public class RoleAssembler {

    private final MenuManager menuManager;

    public RoleVO convertToRoleVo(Role role) {
        List<Menu> menuList = menuManager.findByRoleId(role.getId());
        RoleVO roleVo = new RoleVO();
        roleVo.setRoleId(role.getId());
        roleVo.setName(role.getName());
        roleVo.setCode(role.getCode());
        roleVo.setDescription(role.getDescription());
        roleVo.setPermissions(menuList.stream().map(Menu::getName).toList());
        return roleVo;
    }

    public Role convertToRole(RoleParamDTO roleParamDTO) {
        return convertToRole(new Role(), roleParamDTO);
    }

    public Role convertToRole(Role role, RoleParamDTO roleParamDTO) {
        role.setName(roleParamDTO.getName());
        role.setCode(roleParamDTO.getCode());
        role.setDescription(roleParamDTO.getDescription());
        return role;
    }

}
